package com.nulp.psycho.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ConnectionManager {
	private static final String DRIVER = "jdbc:mysql://localhost:3306/psycho?characterEncoding=UTF-8&characterSetResults=UTF-8";
	private static final String ROOT = "root";
	private static final String PASSWORD = "root";
	private static List<Connection> connectionPool = new ArrayList<>();

	private static ConnectionManager connection = new ConnectionManager(50);

	private ConnectionManager(int count) {
		try {
			new com.mysql.jdbc.Driver();
			for (int i = 0; i < count; i++) {
				connectionPool.add(DriverManager.getConnection(DRIVER + "&user=" + ROOT
						+ "&password=" + PASSWORD));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static ConnectionManager getInstance() {
		return connection;
	}

	public Connection getConnection() {
		Connection con = null;
		if (connectionPool.size() > 0) {
			con = connectionPool.get(0);
			connectionPool.remove(0);
		}
		return con;
	}

	public void release(Connection connection) {
		connectionPool.add(connection);
	}

}
